import os,math ;
from PyQt4.QtCore import QObject,pyqtSignal
from KDataBase.KQueryDB import KQueryDB
from KModel.KSingleStockDataProcessor import KSingleStockDataProcessor as kprocesser
class KSingleStockCursor(QObject):
	'''class to help KSingleStock to hold current position and window related information'''
	def __init__(self,parent=None):
		QObject.__init__(self,parent)	
		

	def GetCurData(self):
		return self.curdata
	
	def GetCurPeriod(self):
		return self.period
	
	def SetTick(self,origdata, timerange=None,period=1):	
		self.timerange=timerange
		self.windowlength=100 # how many bars to display per window
		self.period=int(period) # the time period of stock		
		# filter origdata with period 
		if period!=1:
			self.curdata=kprocesser.SampleDataByPeriod(origdata,period)
		else:
			self.curdata=origdata
		# filter origdata within the timerange
		self.curdata=kprocesser.CropDataByRange(self.curdata,timerange) 
		if len(self.curdata)==0:
			self.curpos=-1 # denote there are no origdata availble
		else:
			self.curpos=max(0,len(self.curdata[0])-self.windowlength) # current cursor position of the data window	
			
	def ChangePeriod(self,origdata,period):
		# change the period (days per bar) of the stock data
		if self.period!=period:
			self.period=period
			self.curdata=kprocesser.SampleDataByPeriod(origdata,period)
			self.curdata=kprocesser.CropDataByRange(self.curdata,self.timerange)
			self.curpos=max(0,len(self.curdata[0])-self.windowlength)
		return True	
	
	def GetTotalLength(self):
		# Get the Length of data at current period
		if len(self.curdata)==0:
			return 0
		else:
			return len(self.curdata[0])
		
	def GetWindowLength(self):
		# Get the Length of Window
		return self.windowlength
	
	def SetWindowLength(self, newlength):
		self.windowlength=newlength
		
	def GetAllowedWindowLength(self):
		return [100,300]
		
	
	def SetCursorPostion(self,newpos):		
		self.curpos=newpos
		
	def GetDatesByIndex(self,IndList):
		result=[]
		if len(self.curdata)==0:
			return []		
		for x in IndList:
			if x >=0 and x< len(self.curdata[0]):
				result.append(self.curdata[0][int(x)])
			else:
				result.append('')
		return result	
	
	def CurWindowRange(self,yrangetype):
		# the vertical range within current stock data series
		startpos=self.curpos
		if startpos<0: return []
		endpos=min(len(self.curdata[0]),startpos+self.windowlength)	
		xdatarange=[startpos,endpos+1]		
		data=zip(*self.curdata[1][startpos:endpos])		
		if  yrangetype=='Price':
			ydatarange=[min(data[2]),max(data[1])]
		elif yrangetype=='Volumn':
			ydatarange=[min(data[4]),max(data[4])]	
		return [xdatarange,ydatarange]	
	
		# get the range of current window
	def CurWindowData(self,stype,AllData=False):
		# get the data filtered by a window 
		if AllData==False:
			startpos=self.curpos
			if startpos<0: return []
			endpos=min(len(self.curdata[0]),startpos+self.windowlength)
		else:
			startpos=0
			if startpos<0: return []
			endpos=len(self.curdata[0])		
		if stype=='Time':
			return self.curdata[0][startpos:endpos]
		elif stype=='Price':
			return self.curdata[1][startpos:endpos]
		elif stype=='Index': # the index of data will be used for drawing
			return range(startpos,endpos)
		elif stype=='Volumn':
			data= zip(*self.curdata[1][startpos:endpos])
			return data[4]


	